package cn.tedu.mvcaddress.controller;

import cn.tedu.mvcaddress.mappers.AddressMapper;
import cn.tedu.mvcaddress.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
 @RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    @RequestMapping("/insert")
    public String insertAddress(Address address){
        addressMapper.insertAddress(address);
        return "添加成功！<a href='/'>返回首页</a>";

    }
    @RequestMapping("/select")
public String selectAddress(){
       List<Address> list= addressMapper.selectAddress();
        String html = "<table border='1'>";
        html += "<caption>地址列表</caption>";
        html += "<tr><th>id</th><th>收件人</th><th>收件地址</th><th>邮箱</th><th>手 机号</th><th>标签</th><th>操作</th></tr>";
        for (Address a: list) {
            html += "<tr>";
            html += "<td>" + a.getId() + "</td>";
            html += "<td>" + a.getReceiver() + "</td>";
            html += "<td>" + a.getAddress() + "</td>";
            html += "<td>" + a.getEmail() + "</td>";
            html += "<td>" + a.getMobile() + "</td>";
            html += "<td>" + a.getTag() + "</td>";
            html += "<td><a href='/address/delete?id=" + a.getId() + "'>删除</a> </td>";
            html += "</tr>";
        }
        html += "</table><a href='/index.html'>返回首页</a>";
        return html;
    }
    @RequestMapping("/update")
    private String updateAddress(Address address){
        addressMapper.updateAddress(address);
        return "修改成功！<a href='/address/select'>返回列表</a>";

    }
    @RequestMapping("/delete")
    public String deleteAddress(int id){
        addressMapper.deleteAddress(id);
        return "删除成功！<a href='/address/select'>返回列表页</a>";
    }

}
